package com.example.innovate.service.impl;

import com.example.innovate.bean.SignRateYear;
import com.example.innovate.bean.SignedStatus;
import com.example.innovate.domain.SigningRateDO;
import com.example.innovate.domain.SigningRateDOExample;
import com.example.innovate.mapper.SigningRateDOMapper;
import com.example.innovate.service.SigningRateService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class SigningRateServiceImpl implements SigningRateService {
    @Autowired
    SigningRateDOMapper signingRateDOMapper;

    @Override
    public SigningRateDO addInfo(SigningRateDO signingRateDO) {
        signingRateDOMapper.insertSelective(signingRateDO);
        return signingRateDO;
    }

    @Override
    public void deleteInfo(int id) {
        signingRateDOMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SigningRateDO updateInfo(SigningRateDO signingRateDO) {
        signingRateDOMapper.updateByPrimaryKeySelective(signingRateDO);
        return signingRateDO;
    }

    @Override
    public List<SigningRateDO> findInfo(String className, int pageNumber, int pageSize) {
        SigningRateDOExample signingRateDOExample = new SigningRateDOExample();
        if(className!=null){
            signingRateDOExample.createCriteria()
                    .andSiCreateClassEqualTo(className);
        }
        PageHelper.startPage(pageNumber,pageSize);
        return signingRateDOMapper.selectByExample(signingRateDOExample);
    }

    @Override
    public SigningRateDO findById(int id) {
        SigningRateDO signingRateDO = signingRateDOMapper.selectByPrimaryKey(id);
        return signingRateDO;
    }

    @Override
    public SignedStatus findSign(String className,String status) {
        SigningRateDOExample signingRateDOExample = new SigningRateDOExample();
        SigningRateDOExample.Criteria criteria = signingRateDOExample.createCriteria();
        if(className!=null){
            criteria.andSiCreateClassEqualTo(className);
        }
        int count1 = signingRateDOMapper.selectByExample(signingRateDOExample).size();
        criteria.andSiSignEqualTo(status);
        int count2 = signingRateDOMapper.selectByExample(signingRateDOExample).size();
        SignedStatus signedStatus = new SignedStatus();
        signedStatus.setContent(status);
        signedStatus.setProportion((count2*1.00)/(count1*1.00));
        return signedStatus;

    }

    @Override
    public SignRateYear findByYear(String year, String status) {
        SigningRateDOExample signingRateDOExample = new SigningRateDOExample();
        SigningRateDOExample.Criteria criteria = signingRateDOExample.createCriteria();
        criteria.andSiTimeLike("%" + year + "%");
        int count1 = signingRateDOMapper.selectByExample(signingRateDOExample).size();
        if (status.equals("未就业")){
            criteria.andSiStateIsNotNull();
        }
        if (status.equals("签约")){
            criteria.andSiSignIsNotNull();
        }
        if (status.equals("非失业")){
            criteria.andSiNonUnemploymentIsNotNull();
        }
        if (status.equals("灵活就业")){
            criteria.andSiFlexibleEmploymentIsNotNull();
        }
        int count2 = signingRateDOMapper.selectByExample(signingRateDOExample).size();
        SignRateYear signRateYear = new SignRateYear();
        signRateYear.setYear(year);
        signRateYear.setContent(status);
        if(count2==0){
            signRateYear.setProportion(0);
        }
        else{
            signRateYear.setProportion((count2*1.00)/(count1*1.00));
        }
        return signRateYear;
    }


}
